/*
gcc test_xrandr_simple.c -lXrandr -lXrender -lX11  -lm -g3
*/
#include "../../../vcd-common-api/vcd-common.h"
#include "vcd-xrandr.h"
/*
static const char *connection[3] = {
    "connected",
    "disconnected",
    "unknown connection"};
*/
#ifdef HAVE_XRANDR
static XRRScreenResources  *get_screen (Bool current, Display *dpy, Window root)
{
    XRRScreenResources  *res;

//    XRRGetScreenSizeRange(dpy, root, &minWidth, &minHeight,
//                           &maxWidth, &maxHeight);
    
    if (current)
        res = XRRGetScreenResourcesCurrent (dpy, root);
    else
        res = XRRGetScreenResources (dpy, root);
    if (!res) 
        printf ("could not get screen resources\n");
    return res;
}

static XRRModeInfo *find_mode (RRMode xid, XRRScreenResources  *res)
{
    int	m;
    XRRModeInfo	*best = NULL;

    for (m = 0; m < res->nmode; m++) {
        XRRModeInfo *mode = &res->modes[m];
        if ( xid == mode->id) {
            best = mode;
            break;
        }
    }
    return best;
}


DisOut* get_dis_out(void)
{
    int o = 0;
    int k = 0;

    Display	*dpy;
    Window	root;
    int	screen = -1;
    XRRScreenResources  *res;
    char* display_name = NULL;

    DisOut* dis_out =  NULL;
    OutPut* output = NULL;

    dis_out = calloc(1, sizeof(DisOut));
    if (dis_out == NULL) {
       goto fail;
    }

    dpy = XOpenDisplay(display_name);

    if (dpy == NULL) {
        LOG_ERROR("Display failed!");
        return NULL;
    }

    dis_out->dpy = dpy;

    screen = DefaultScreen (dpy);

    root = RootWindow (dpy, screen);

    res = get_screen(1, dpy, root);
    dis_out->res = res;

    //add output data
    output = calloc(res->noutput, sizeof(OutPut));
    if (output == NULL) {
        M_DEBUG("output = NULL");
        return NULL;
    }

    dis_out->output = output;
    dis_out->noutput = res->noutput;

    //get current display width and height
    dis_out->cur_width  = DisplayWidth(dpy, screen);
    dis_out->cur_height = DisplayHeight(dpy, screen);

    for (o = 0; o < res->noutput; o++)
    {
        XRROutputInfo	*output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
        OutPut* out = output + o;
        out->name = strdup(output_info->name);
        if (out->name == NULL) {
            M_DEBUG("out->name = NULL");
            goto fail;
        }

        out->connected = (OutputConnectStatus)output_info->connection;

       // printf ("%s %s %d\n", output_info->name, connection[output_info->connection], output_info->nmode);

        int j = 0;
        out->modes = (char**)calloc(output_info->nmode, sizeof(char*));
        if (out->modes == NULL) {
            goto fail;
        }
        out->nmode = 0;
        for (j = 0; j < output_info->nmode; j++)
        {
            RRMode mode = output_info->modes[j];
            XRRModeInfo * jmode = find_mode(mode, res);

            for(k = 0; k < out->nmode; ++k){
                if(strcmp(out->modes[k], jmode->name) == 0){
                    break;
                }
            }

            //exist
            if (k != out->nmode) {
                continue;
            }

            //add to list
            out->modes[out->nmode] = strdup(jmode->name);
            ++out->nmode;
        }
    }

    return dis_out;

fail:
    destroy_dis_out(dis_out);
    return NULL;
}

void destroy_dis_out(DisOut* dis_out)
{
    if (dis_out == NULL)
        return;

    XCloseDisplay(dis_out->dpy);
    dis_out->dpy = NULL;

    int i = 0, j = 0;
    for (i = 0; i < dis_out->noutput; ++i) {
        OutPut* output = &dis_out->output[i];
        for (j = 0; j < output->nmode; ++j) {
            SAFE_FREE(output->modes[j]);
        }
        SAFE_FREE(output->name);
        SAFE_FREE(output->modes)
    }
    free(dis_out);
    dis_out = NULL;
}
#endif